//----
// PART V - Comparing Jurisdictions


	/* This file uses Schedule K-1 data with the jurisdiction
	of the recipient to describe patterns across industry,
	character of income, and type of recipient. It also
	produces Figures 7, 8, 9, and 14. */

// ----------------
// Comparison of income shares across jurisdictions
		
	// -----------------
	// Compare industry
	
		use ${data_dir}/income_table.dta, clear
		
		drop if year<2011
		
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// create large industry bins
			
			/* Payer industry is 2-digit NAICS plus a special
			category "IV" for 5239 investment funds. */
			
			g industry = ""
			
			// Finance
			replace industry = "Fin" if payer_industry=="52" ///
				| payer_industry=="53" ///
				| payer_industry=="55"
			
			// Professional Services
			replace industry = "Pro" if payer_industry=="54"
			
			// Manufacturing
			replace industry = "Man" if payer_industry=="31" ///
				| payer_industry=="32" ///
				| payer_industry=="33"
					
			// Oil and Gas
			replace industry = "Oil" if payer_industry=="21"
			
			// Investment (NAICS 5239)
			replace industry = "Inv" if payer_industry=="IV" 
			
			// Other
			replace industry = "Oth" if industry==""
		
		// Collapse 
		
		gcollapse (sum) net_income, by(destination industry)
		
		// Create shares by destination
		
			local d_list "Inv Fin Pro Man Oil Oth"
			tokenize "`d_list'"
			local `1' "Cay"
			local `2' "Zer"
			local `3' "Con"
			local `4' "Hav"
			local `5' "For"
			local `6' "US"
			g place = ""
			forvalues j = 1/6 {
				replace place = "```j'''" if _n==`j'
				}
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				g s``xx'' = .
				forvalues j = 1/7 {
					sum net_income if dest=="```j'''" & industry=="``xx''"
					replace s``xx'' = r(mean)*r(N) if _n==`j'
					}
				}
						
			g stot = 0
			forvalues xx = 1/`nn' {
				replace stot = stot+s``xx''
				}
			forvalues xx = 1/`nn' {
				g p``xx'' = s``xx''/stot*100
				}
		
		// Set order and labels
		
			g ord = 1 if place == "Cay"
			replace ord = 2 if place == "Zer"
			replace ord = 3 if place == "Con"
			replace ord = 4 if place == "Hav"
			replace ord = 5 if place == "For"
			replace ord = 6 if place == "US"
			
			g olabel = ""
			replace olabel = "Cayman Isl" if place=="Cay"
			replace olabel = "Zero Haven" if place=="Zer"
			replace olabel = "Conduit" if place=="Con"
			replace olabel = "Oth Haven" if place=="Hav"
			replace olabel = "Oth Foreign" if place=="For"
			replace olabel = "USA" if place=="US"
			
		// Figure 7	
			
		// Clean	
			
			keep if _n<=6
			
			order ord olabel pInv-pOth
			
			keep ord olabel pInv-pOth
			
		
		// grayscale
		
		labmask ord, values(olabel)
		graph bar pInv pFin pPro pMan pOil pOth, ///
			over(ord, gap(10) label(labsize(msmall))) ///
			stack ///
			bar(1, color(gray*4)) ///
			bar(2, color(gray*1.75)) ///
			bar(3, color(gray*1.1)) ///
			bar(4, color(gray*.8)) ///
			bar(5, color(gray*.5)) ///
			bar(6, color(gray*.25)) ///
			///bar(7, color(gray)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Share of Income", height(8)) ///
			leg(pos(6) cols(3) order(1 "Investment" 2 "Finance" 3 "Prof. Serv." 4 "Manuf." 5  "Oil & Gas" 6 "Other") size(msmall) region(lcolor(white)))		
			graph export ${fig_dir}/fig_sharecompare_industry.png, replace width(1000)	
			
		// color
		
		labmask ord, values(olabel)
		graph bar pInv pFin pPro pMan pOil pOth, ///
			over(ord, gap(10) label(labsize(msmall))) ///
			stack ///
			bar(1, color(navy*.9)) ///
			bar(2, color("70 122 54")) ///
			bar(3, color(sand)) ///
			bar(4, color(dkorange)) ///
			bar(5, color("180 62 62")) ///
			bar(6, color("147 109 163")) ///
			///bar(7, color(gray)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Share of Income", height(8)) ///
			leg(pos(6) cols(3) order(1 "Investment" 2 "Finance" 3 "Prof. Serv." 4 "Manuf." 5  "Oil & Gas" 6 "Other") size(msmall) region(lcolor(white)))		
			graph export ${fig_dir}/fig_sharecompare_industry_color.png, replace width(1000)		
		
		
		// Export

			foreach v of varlist pInv-pOth {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig7_tab.csv, replace
		
		
	// -----------------
	// Compare class of recipient by destination
	
		use ${data_dir}/income_table.dta, clear
		
		drop if year<2011
		
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// clear unknown recipients
		
		drop if class==""
		
		// Include S corporations as C corporations
		
		replace class="C" if class=="S"
		
		// Collapse
		
		gcollapse (sum) net_income, by(destination class_entity)
		
		// Create shares by destination
			
			local d_list "Z C Y N D T H"
			tokenize "`d_list'"
			local `1' "Cay"
			local `2' "Zer"
			local `3' "Con"
			local `4' "Hav"
			local `5' "For"
			local `6' "US"
			g place = ""
			forvalues j = 1/6 {
				replace place = "```j'''" if _n==`j'
				}
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				g s``xx'' = .
				forvalues j = 1/6 {
					sum net_income if dest=="```j'''" & class=="``xx''"
					replace s``xx'' = r(mean)*r(N) if _n==`j'
					}
				}
			
			g stot = 0
			forvalues xx = 1/`nn' {
				replace stot = stot+s``xx''
				}
			forvalues xx = 1/`nn' {
				g p``xx'' = s``xx''/stot*100
				}
		
		// Set order and labels 
		
			g ord = 1 if place == "Cay"
			replace ord = 2 if place == "Zer"
			replace ord = 4 if place == "Con"
			replace ord = 5 if place == "Hav"
			replace ord = 6 if place == "For"
			replace ord = 7 if place == "US"
			
			g olabel = ""
			replace olabel = "Cayman Isl" if place=="Cay"
			replace olabel = "Zero Haven" if place=="Zer"
			replace olabel = "Conduit" if place=="Con"
			replace olabel = "Oth Haven" if place=="Hav"
			replace olabel = "Oth Foreign" if place=="For"
			replace olabel = "USA" if place=="US"
		
		// Figure 8	
			
		// Clean
			
			keep if _n<=6
			
			order ord olabel pZ-pH
			
			keep ord olabel pZ-pH
			
		
		// grayscale
		
		labmask ord, values(olabel)
		graph bar pZ pC pY pN pD pT pH, ///
			over(ord, gap(10) label(labsize(msmall))) ///
			stack ///
			bar(1, color(gray*4)) ///
			bar(2, color(gray*2)) ///
			bar(3, color(gray*1.5)) ///
			bar(4, color(gray*1)) ///
			bar(5, color(gray*.75)) ///
			bar(6, color(gray*.5)) ///
			bar(7, color(gray*.25)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Share of Income", height(8)) ///
			leg(pos(6) cols(4) order(1 "Entity" 2 "Corporation" 3 "Hybrid" 4 "Exempt/Gov" 5 "Custodial" 6 "Trust/Estate" 7 "Individual") size(msmall) region(lcolor(white)))			
			graph export ${fig_dir}/fig_sharecompare_class.png, replace width(1000)		
			
			
		// color
		
		labmask ord, values(olabel)
		graph bar pZ pC pY pN pD pT pH, ///
			over(ord, gap(10) label(labsize(msmall))) ///
			stack ///
			bar(1, color(gray)) ///
			bar(2, color(navy*.9)) ///
			bar(3, color("70 122 54")) ///
			bar(4, color(sand)) ///
			bar(5, color(dkorange)) ///
			bar(6, color("180 62 62")) ///
			bar(7, color("147 109 163")) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Share of Income", height(8)) ///
			leg(pos(6) cols(4) order(1 "Entity" 2 "Corporation" 3 "Hybrid" 4 "Exempt/Gov" 5 "Custodial" 6 "Trust/Estate" 7 "Individual") size(msmall) region(lcolor(white)))			
			graph export ${fig_dir}/fig_sharecompare_class_color.png, replace width(1000)		

		// Export

			foreach v of varlist pZ-pH {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig8_tab.csv, replace
		
		
			
	// -----------------
	// Compare character of income by destination
	
		use ${data_dir}/income_table.dta, clear
		
		drop if year<2011
		
		// exclude income flowing to higher-tier partnerships
		
		drop if class_entity == "P"		
		
		// Consolidate categories of income
		
		g ordinary = box1_ordinary
		g realestate = box2_netrental+ box3_othernetrental
		g interest = box5_interest
		g royalties = box7_royalties
		g dividends = box6a_dividends
		g capgains = box8_netstcap+box9a_netltcap
		
		// Collapse
		
			gcollapse (sum) ordinary-capgains, by(destination)
				
			local d_list "ord rea inte roy div cap"
			tokenize "`d_list'"
			local `1' "Cay"
			local `2' "Zer"
			local `3' "Con"
			local `4' "Hav"
			local `5' "For"
			local `6' "US"
			g place = ""
			forvalues j = 1/6 {
				replace place = "```j'''" if _n==`j'
				}
			local nn: word count `d_list'
			forvalues xx = 1/`nn' {
				g s``xx'' = .
				forvalues j = 1/6 {
					sum ``xx'' if dest=="```j'''"
					replace s``xx'' = r(mean)*r(N) if _n==`j'
					}
				}
			
			g stot = 0
			forvalues xx = 1/`nn' {
				replace stot = stot+s``xx''
				}
			forvalues xx = 1/`nn' {
				g p``xx'' = s``xx''/stot*100
				}
		
		// Set order and labels
		
			g ord = 1 if place == "Cay"
			replace ord = 2 if place == "Zer"
			replace ord = 3 if place == "Con"
			replace ord = 4 if place == "Hav"
			replace ord = 5 if place == "For"
			replace ord = 6 if place == "US"
			
			g olabel = ""
			replace olabel = "Cayman Isl" if place=="Cay"
			replace olabel = "Zero Haven" if place=="Zer"
			replace olabel = "Conduit" if place=="Con"
			replace olabel = "Oth Haven" if place=="Hav"
			replace olabel = "Oth Foreign" if place=="For"
			replace olabel = "USA" if place=="US"
		
		// Figure 9	
			
		// Clean
		
			
			keep if _n<=6
			
			order ord olabel pord-pcap
			
			keep ord olabel pord-pcap
			
		// grayscale
		
		labmask ord, values(olabel)
		graph bar pord prea proy pdiv pinte pcap, ///
			over(ord, gap(10) label(labsize(msmall))) ///
			stack ///
			bar(1, color(gray*.25)) ///
			bar(2, color(gray*.5)) ///
			bar(3, color(gray*.8)) ///
			bar(4, color(gray*1.1)) ///
			bar(5, color(gray*1.75)) ///
			bar(6, color(gray*4)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Share of Income", height(8)) ///
			leg(pos(6) cols(3) order(1 "Ordinary" 2 "Real Est." 3 "Royalty" 4 "Dividend" 5 "Interest" 6 "Cap Gain") size(msmall) region(lcolor(white)))			
			graph export ${fig_dir}/fig_sharecompare_inctype.png, replace width(1000)		
		
		// color
		
		labmask ord, values(olabel)
		graph bar pord prea proy pdiv pinte pcap, ///
			over(ord, gap(10) label(labsize(msmall))) ///
			stack ///
			bar(1, color(navy*.9)) ///
			bar(2, color("70 122 54")) ///
			bar(3, color(sand)) ///
			bar(4, color(dkorange)) ///
			bar(5, color("180 62 62")) ///
			bar(6, color("147 109 163")) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Share of Income", height(8)) ///
			leg(pos(6) cols(3) order(1 "Ordinary" 2 "Real Est." 3 "Royalty" 4 "Dividend" 5 "Interest" 6 "Cap Gain") size(msmall) region(lcolor(white)))			
			graph export ${fig_dir}/fig_sharecompare_inctype_color.png, replace width(1000)		
		
		// Export
		
			foreach v of varlist pord-pcap {
				replace `v'= round(`v',.1)
				}
		
			export delimited using ${out_dir}/fig9_tab.csv, replace
		
		

// ----------------
// Variation of flows by destination over time
		
	foreach z in "US" "For" "Cay" {
	
	global category "`z'"
		
	//------------------------------------------------------
	// Shares by industry over time
	
		clear
		
		use ${data_dir}/income_table.dta, clear
	
		// exclude income flowing to higher-tier partnerships
	
		drop if class_entity == "P"		

		// create large industry bins
		
			g industry = ""
			
			// Finance
			replace industry = "Fin" if payer_industry=="52" ///
				| payer_industry=="53" ///
				| payer_industry=="55"
			
			// Professional Services
			replace industry = "Pro" if payer_industry=="54"
			
			// Manufacturing
			replace industry = "Man" if payer_industry=="31" ///
				| payer_industry=="32" ///
				| payer_industry=="33"
					
			// Oil and Gas
			replace industry = "Oil" if payer_industry=="21"
			
			// Investment (NAICS 5239)
			replace industry = "Inv" if payer_industry=="IV" 
			
			// Other
			replace industry = "Oth" if industry==""
		
		
		// Isolate desired category
		
		if "${category}"=="For" {
			replace destination = "For" ///
				if inlist(destination,"Cay","Hav","Con","Zer","Und")
			}
		
		keep if destination=="${category}"
		
		// Collapse 
		
		gcollapse (sum) net_income, by(industry year)
		
		// Calculate share of totals by year
		
		egen yrtot = sum(net_income), by(year)
		g share = net_income/yrtot
		replace share = share*100
		
		// Organize data for graph
		
		g y = 2005-1+_n 
			replace y = . if y>2019
		
		local d_list "Inv Fin Pro Man Oil Oth"
		tokenize "`d_list'"
		local nn: word count `d_list'
		forvalues xx = 1/`nn' {
			sum share if year==2005 & ind=="``xx''"
			g ``xx'' = r(mean) if y==2005
			}
		local t 2006
		forvalues xx = 1/`nn' {
			forvalues y = `t'/2019 {
				sum share if year==`y' & ind=="``xx''"
				replace ``xx'' = r(mean) if y==`y'
				}		
			}
		
		//------------------------------------------------------
		// Panels (a) (c) and (e) of Figure 14

		// Stacking
		
		replace Fin = Fin+Inv
		replace Pro = Fin+Pro
		replace Man = Pro+Man
		replace Oil = Oil+Man 
		replace Oth = 100 if Oth<.
		
		keep Inv Fin Pro Man Oil Oth y
		keep if y<.

		
		// grayscale
		
		twoway ///
			(area Inv y, color(gray*8) lwidth(none)) ///
			(rarea Fin Inv y, color(gray*3) lwidth(none)) ///
			(rarea Pro Fin y, color(gray*2) lwidth(none)) ///
			(rarea Man Pro y, color(gray*1.5) lwidth(none)) ///
			(rarea Oil Man y, color(gray*1) lwidth(none)) ///
			(rarea Oth Oil y, color(gray*.5) lwidth(none)), ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Percentage of Net Income", height(8)) ///
			ylabel(0(20)100, nogrid ) ///
			xlabel(2005(2)2019) ///
			xtitle("") ///
			leg(pos(6) cols(3) order(1 "Investment" 2 "Finance" 3 "Prof. Serv." 4 "Manuf." 5 "Oil & Gas" 6 "Other") region(lcolor(white)))	
			graph export  ${fig_dir}/fig_sharesovertime_${category}_industry.png, replace width(1000)
		
		// color
		
		twoway ///
			(area Inv y, color(navy) lwidth(none)) ///
			(rarea Fin Inv y, color("70 122 54") lwidth(none)) ///
			(rarea Pro Fin y, color(sand) lwidth(none)) ///
			(rarea Man Pro y, color(dkorange) lwidth(none)) ///
			(rarea Oil Man y, color(red*1.5) lwidth(none)) ///
			(rarea Oth Oil y, color("119 69 140") lwidth(none)), ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Percentage of Net Income", height(8)) ///
			ylabel(0(20)100, nogrid ) ///
			xlabel(2005(2)2019) ///
			xtitle("") ///
			leg(pos(6) cols(3) order(1 "Investment" 2 "Finance" 3 "Prof. Serv." 4 "Manuf." 5 "Oil & Gas" 6 "Other") region(lcolor(white)))	
			graph export  ${fig_dir}/fig_sharesovertime_${category}_industry_color.png, replace width(1000)

		
		// Export

			foreach v of varlist Inv-Oth {
				replace `v'= round(`v',.1)
				}
		
			if "${category}"=="US" {
				local panel "a"
			}
		
			if "${category}"=="For" {
				local panel "c"
			}
		
			if "${category}"=="Cay" {
				local panel "e"
			}
		
			export delimited using ${out_dir}/fig14`panel'_tab.csv, replace

		
	//------------------------------------------------------
	// Shares by character of income over time
	
		clear
				
		use ${data_dir}/income_table.dta, clear
	
		// exclude income flowing to higher-tier partnerships
	
		drop if class_entity == "P"			
		
		// Isolate desired category
		
		if "${category}"=="For" {
			replace destination = "For" ///
				if inlist(destination,"Cay","Hav","Con","Zer","Und")
			}
		
		keep if destination=="${category}"
		
		// Collapse 
				
		gcollapse (sum) box*, by(year)
		
		// Consolidate characters of income
		
		g ordinary = box1_ordinary
		g realestate = box2_netrental+ box3_othernetrental
		g interest = box5_interest
		g royalties = box7_royalties
		g dividends = box6a_dividends
		g capgains = box8_netstcap+box9a_netltcap
		
		// Calculate shares
		
		g tot = ord + rea + inte + roy + div + cap
		
		foreach var of varlist ord-cap {
			replace `var' = `var'/tot*100
			}

		
		//------------------------------------------------------
		// Panels (b) (d) and (f) of Figure 14
		
		// Stack
		
		replace rea = rea+ord
		replace roy = rea+roy
		replace div = div+roy
		replace inte = inte+div
		replace cap = 100
		
		rename year y
		
		keep y ord-cap
		
		keep if y<.
		
		// grayscale
		
		twoway ///
			(area ord y, color(gray*.5) lwidth(none)) ///
			(rarea rea ord y, color(gray*1) lwidth(none)) ///
			(rarea roy rea y, color(gray*1.5) lwidth(none)) ///
			(rarea div roy y, color(gray*2) lwidth(none)) ///
			(rarea inte div y, color(gray*3) lwidth(none)) ///
			(rarea cap inte y, color(gray*8) lwidth(none)), ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Percentage of Net Income", height(8)) ///
			ylabel(0(20)100, nogrid ) ///
			xlabel(2005(2)2019) ///
			xtitle("") ///
			leg(pos(6) cols(3) order(1 "Ordinary" 2 "Real Est." 3 "Royalty" 4 "Dividend" 5 "Interest" 6 "Cap Gain") region(lcolor(white)))			
			graph export ${fig_dir}/fig_sharesovertime_${category}_inctype.png, replace width(1000)
		
		
		// color
		
		twoway ///
			(area ord y, color(navy) lwidth(none)) ///
			(rarea rea ord y, color("70 122 54") lwidth(none)) ///
			(rarea roy rea y, color(sand) lwidth(none)) ///
			(rarea div roy y, color(dkorange) lwidth(none)) ///
			(rarea inte div y, color(red*1.5) lwidth(none)) ///
			(rarea cap inte y, color("119 69 140") lwidth(none)), ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Percentage of Net Income", height(8)) ///
			ylabel(0(20)100, nogrid ) ///
			xlabel(2005(2)2019) ///
			xtitle("") ///
			leg(pos(6) cols(3) order(1 "Ordinary" 2 "Real Est." 3 "Royalty" 4 "Dividend" 5 "Interest" 6 "Cap Gain") region(lcolor(white)))			
			graph export ${fig_dir}/fig_sharesovertime_${category}_inctype_color.png, replace width(1000)
		
		
		// Export

			foreach v of varlist ord-cap {
				replace `v'= round(`v',.1)
				}
		
			if "${category}"=="US" {
				local panel "b"
			}
		
			if "${category}"=="For" {
				local panel "d"
			}
		
			if "${category}"=="Cay" {
				local panel "f"
			}
		
			export delimited using ${out_dir}/fig14`panel'_tab.csv, replace

}
